<template>
  <view>
    <view class="page">
      <image @click="inSeeImg" class="top-img" mode="aspectFill" :src="thisqueryobj"
        :style="'height:'+sys.screenHeight*1.81+'rpx;width:100%'"></image>
      <!-- 商品导航组件 -->
      <view class="goods_nav">
        <uni-goods-nav :fill="true" :options="options" :button-group="customButtonGroup" @click="onClick"
          @buttonClick="buttonClick" />
      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapGetters('m_cart', ['total'])
    },
    watch: {
      total: {
        handler(newValue) {
          console.log(newValue);
          const findResult = this.options.find((x) => x.text === '壁纸集')
          if (findResult) {
            findResult.info = newValue
          }
        },
        immediate: true
      }
    },
    data() {
      return {
        thisqueryobj: [],
        sys: uni.getSystemInfoSync(),
        isIPX: getApp().isIPX,
        customButtonGroup: [{
          text: '下载壁纸',
          backgroundColor: 'linear-gradient(160deg, #b100ff 20%,#00b3ff 80%);',
          color: '#fff'
        }],
        options: [{
          icon: 'contact-filled',
          text: '我的'
        }, {
          icon: 'shop',
          text: '壁纸集',
          infoColor: "#f5f5f5",
          info: 0,
          infoBackgroundColor: '#007aff',
        }, {
          icon: 'camera-filled',
          text: '加入壁纸集',
        }],
      };
    },
    onLoad(options) {
      uni.$showMsg("点击图片进行预览")
      if (options.src) {
        console.log(options.src);
        var o = options.src.split("q70")[0].slice(-4),
          n = decodeURIComponent(options.src.replace(o, "w1080"));
        // split分割字符串，slice总第几项开始截取。所以o,输出的值是w244,也就是四位数
        // replace替换字符集。所以把src字符串的特定字符替换成w1080.然后使用decodeURIComponent方法进行解码
        -1 != n.indexOf("mi-img") && (n = n.replace("mi-img", "xiaomi")),
        this.thisqueryobj = n || '';
        // 用indexof判断是否有“mi-img”，这个特定的字符，如果有&&用replace替换字符。然后调用this.setdata()把得到的src进行默认值的替换；
      }
      console.log(this.thisqueryobj);
    },
    methods: {
      ...mapMutations('m_cart', ['addTocart']),
      inSeeImg() {
        var e = this.thisqueryobj.src;
        uni.previewImage({
          current: e,
          urls: [e]
        });
      }, //全屏操纵
      onClick(e) {
        if (e.content.text === '我的') return uni.switchTab({
          url: "/pages/my/my"
        });
        if (e.content.text === '壁纸集') return uni.switchTab({
          url: "/pages/cart/cart"
        });
        if (e.content.text == '加入壁纸集') {
          const goods = {
            goods_src: this.thisqueryobj.src,
            goods_title: this.thisqueryobj.title,
            goods_state: false
          }
          this.addTocart(goods)
        }
      },
      buttonClick(e) {
        if (e.index === 0) {
          var imgactive = this.thisqueryobj;
          uni.showLoading({
              title: "保存中....",
              mask: !0
            }),
            uni.getImageInfo({
              src: "https://" + imgactive.replace(imgactive.split(".market")[0], "f6"),
              success: function(e) {
                uni.hideLoading();
                var t = e.path;
                uni.saveImageToPhotosAlbum({
                  filePath: e.path,
                  success: function() {
                    uni.showToast({
                      title: '保存成功',
                      icon: "none"
                    })
                  },
                  fail: function(e) {
                    uni.hideLoading(),
                      uni.showModal({
                        content: "授权后才能下载到本地，是否重新授权？",
                        success: function() {
                          e.confirm && uni.openSetting({
                            success: function(e) {
                              e.authSetting["scope.writePhotosAlbum"] && uni
                                .saveImageToPhotosAlbum({
                                  filePath: t,
                                  success: function(e) {
                                    uni.showToast({
                                      title: "保存成功",
                                      icon: "none"
                                    })
                                  }
                                })
                            }
                          })
                        }
                      })
                  }
                })
              },
              fail: function(e) {
                uni.$showMsg('请稍后重试~')
                uni.hideLoading();
              }
            })
        } else {
          this.$showMsg('请稍后重试~')
        }
      }
    }
  }
</script>

<style lang="scss">
  .top-bar {
    background: none;
  }

  .goods_nav {
    // 为商品导航组件添加固定定位
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }

  f .bom-bar {
    position: fixed;
    width: 100%;
    height: 88rpx;
    bottom: 0;
    left: 0;
    font-size: 22rpx;
    text-align: center;
    color: rgba(255, 255, 255, .9);
    background-color: rgba(0, 0, 0, .4);
  }

  .bom-bar>view {
    position: relative;
  }

  .bom-bar .iconfont {
    position: relative;
    z-index: 2;
    display: inline-block;
    font-size: 36rpx;
    padding: 6rpx 0;
  }

  .down .iconfont {
    position: relative;
    width: 76rpx;
    height: 76rpx;
    line-height: 54rpx;
    border-radius: 50%;
    top: -38rpx;
    background-color: #f5c900;
  }

  .down>view {
    margin-top: -24rpx;
  }

  .down::before {
    content: '';
    display: block;
    position: absolute;
    top: 0;
    left: 50%;
    margin: -44rpx 0 0 -44rpx;
    width: 88rpx;
    height: 44rpx;
    background-color: rgba(0, 0, 0, .4);
    border-radius: 44rpx 44rpx 0 0;
  }

  .custom-btn {
    height: auto !important;
    line-height: inherit !important;
    padding: 0 !important;
    background-color: initial !important;
    font-size: inherit !important;
    color: #fff !important;
  }
</style>
